package com.ztf.dao;

import com.ztf.bean.Message;
import com.ztf.bean.User;
import com.ztf.until.ConnectionManager;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class MsgDao {

    public static void addMsg(List<Message> msgs){
        Connection conn = null;
        PreparedStatement prep =null;
        String sql = "insert into messages values(?,?,?,?)";
        try {
            conn = ConnectionManager.getConnection();
            prep = conn.prepareStatement(sql);
            for (Message msg : msgs) {
                prep.setString(1,msg.getFrom().getName());
                prep.setString(2,msg.getTo().getName());
                prep.setLong(3,msg.getTime());
                prep.setString(4,msg.getContent());
                //加入批处理
                prep.addBatch();
            }
            //执行批处理
            prep.executeBatch();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            ConnectionManager.closeConnection(prep,conn);
        }
    }

    public static List<Message> readMsg(User user){

        Connection conn = null;
        PreparedStatement prep =null;
        ResultSet resultSet = null;
        List<Message> list = new ArrayList<>();
        String sql = "select * from messages where from_user=? or to_user=? order by send_time asc ";
        try {

            InputStream is  = new FileInputStream("service/head/pic1.jpg");
            int len = is.available();
            byte[] head = new byte[len];
            is.read(head);

            conn = ConnectionManager.getConnection();
            prep = conn.prepareStatement(sql);
            prep.setString(1,user.getName());
            prep.setString(2,user.getName());
            resultSet = prep.executeQuery();
            while (resultSet.next()){
                String from = resultSet.getString(1);
                String to = resultSet.getString(2);
                long time = resultSet.getLong(3);
                String content = resultSet.getString(4);

                User u_from = new User();
                User u_to = new User();
                if (from.equals(user.getName())){
                    u_from.setName(user.getName());
                    u_from.setHead(user.getHead());
                    u_from.setIdentity(user.getIdentity());

                    u_to.setName(to);
                    u_to.setIdentity("客服");
                    u_to.setHead(head);
                    Message message = new Message(u_from,u_to,time,content);
                    list.add(message);
                }else {
                    u_from.setName(from);
                    u_from.setIdentity("客服");
                    u_from.setHead(head);

                    u_to.setName(user.getName());
                    u_to.setIdentity(user.getIdentity());
                    u_to.setHead(user.getHead());
                    Message message = new Message(u_from,u_to,time,content);
                    list.add(message);
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            ConnectionManager.closeConnection(prep,conn);
        }
        return list;
    }

    public static void saveService(String who_service,String to_service,long service_time){
        Connection conn = null;
        PreparedStatement prep =null;
        String sql = "insert into service_person values(?,?,?)";

        try {
            conn = ConnectionManager.getConnection();
            prep = conn.prepareStatement(sql);

            prep.setString(1,who_service);
            prep.setString(2,to_service);
            prep.setLong(3,service_time);

            prep.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            ConnectionManager.closeConnection(prep,conn);
        }
    }
}
